﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data.SqlClient;
using eschoolBO;

namespace ESCHOOL.admin
{
    public partial class WebForm15 : System.Web.UI.Page
    {
        timetable tt = new timetable();
        teacher teach = new teacher(); 
        Classes cl = new Classes(); 
        subject sub = new subject();
        protected void Page_Load(object sender, EventArgs e)
        {
            btnupdate.Visible = false;
            if (!IsPostBack)
            {
                
                DataTable dt1 = teach.GetRecords(-99,"","","","","","",-99,-99,-99,"","","","","",false," and ");
                ddlteachid.DataSource = dt1;
                ddlteachid.DataTextField = "teacher_name";
                ddlteachid.DataValueField = "teacher_id";
                ddlteachid.DataBind();

                DataTable dt2 = cl.GetRecords(-99, "", -99, false, " and ");
                ddlclassid.DataSource = dt2;
                ddlclassid.DataTextField = "class_name";
                ddlclassid.DataValueField = "class_id";
                ddlclassid.DataBind();

                DataTable dt3 = sub.GetRecords(-99, "", -99, -99, false, " and ");
                ddlsubjectid.DataSource = dt3;
                ddlsubjectid.DataTextField = "subject_name";
                ddlsubjectid.DataValueField = "subject_id";
                ddlsubjectid.DataBind();

                DataTable dt = tt.GetRecords(-99,-99,-99,-99,"","","",false," and ");
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }

        protected void btndelete_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                DataTable dt = tt.GetRecords(Convert.ToInt64(e.CommandArgument), -99, -99, -99, "", "", "", false, " and ");
                tt.time_table_id = Convert.ToInt64(dt.Rows[0]["time_table_id"].ToString());
                tt.teacher_id =Convert.ToInt64(dt.Rows[0]["teacher_id"].ToString());
                tt.class_id = Convert.ToInt64(dt.Rows[0]["class_id"].ToString());
                tt.dayname = dt.Rows[0]["dayname"].ToString();
                tt.subject_id = Convert.ToInt64(dt.Rows[0]["subject_id"].ToString());
                tt.starttime = dt.Rows[0]["starttime"].ToString();
                tt.endtime = dt.Rows[0]["endtime"].ToString();
                tt.isdeleted = true;
                tt.SaveRecords();
                Response.Redirect("~/admin/TimeTable.aspx");
            }
        }

        protected void btnedit_Command(object sender, CommandEventArgs e)
        {
            btninsert.Visible = false;
            btnupdate.Visible = true;
            DataTable dt = tt.GetRecords(Convert.ToInt64(e.CommandArgument), -99, -99, -99, "", "", "", false, " and ");
            tbtimetableid.Text = dt.Rows[0]["time_table_id"].ToString();
            ddlteachid.SelectedValue = dt.Rows[0]["teacher_id"].ToString();
            ddlclassid.SelectedValue = dt.Rows[0]["class_id"].ToString();
            ddldayname.SelectedValue = dt.Rows[0]["dayname"].ToString();
            ddlsubjectid.SelectedValue = dt.Rows[0]["subject_id"].ToString();
            //ddldayname.SelectedValue = dt.Rows[0]["day_name"].ToString();
            tbstarttime.Text = Convert.ToDateTime(dt.Rows[0]["starttime"].ToString()).ToLongDateString();
            tbendtime.Text = Convert.ToDateTime(dt.Rows[0]["endtime"].ToString()).ToLongDateString();

        }

        protected void btninsert_Click(object sender, EventArgs e)
        {
            //DataTable dt = tt.GetRecords(-99, -99, -99, "", "", "", "", false, " and ");
            tt.time_table_id = -99;
            tt.teacher_id=Convert.ToInt64(ddlteachid.SelectedValue);
            tt.class_id = Convert.ToInt64(ddlclassid.SelectedValue);
            tt.subject_id = Convert.ToInt64(ddlsubjectid.SelectedValue);
            tt.dayname = ddldayname.SelectedValue;
            tt.starttime = tbstarttime.Text;
            tt.endtime = tbendtime.Text;
            tt.isdeleted = false;
            tt.SaveRecords();
            Response.Redirect("~/admin/TimeTable.aspx");
        }

        protected void btnupdate_Click(object sender, EventArgs e)
        {
            tt.time_table_id=Convert.ToInt64(tbtimetableid.Text);
            tt.teacher_id = Convert.ToInt64(ddlteachid.SelectedValue);
            tt.class_id = Convert.ToInt64(ddlclassid.SelectedValue);
            tt.subject_id = Convert.ToInt64(ddlsubjectid.SelectedValue);
            tt.dayname = ddldayname.SelectedValue;
            tt.starttime = tbstarttime.Text;
            tt.endtime = tbendtime.Text;
            tt.isdeleted = false;
            tt.SaveRecords();
            Response.Redirect("~/admin/TimeTable.aspx");

        }

        

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }     
    }
}
